import { io } from "socket.io-client";
import { SocketType } from "./type";

const socket: SocketType = io("http://localhost:3000");

const formMsg = document.querySelector("form#form-msg") as HTMLFormElement;
const inputMsg = document.querySelector("input#msg") as HTMLInputElement;

formMsg.onsubmit = (ev) => {
  ev.preventDefault();
  if (inputMsg.value) {
    socket.emit("chat_msg", inputMsg.value);
    inputMsg.value = "";
  }
};

socket.on("chat_msg", (msg) => {
  console.log(`received message: ${msg}`);
});
